<?php
namespace app\zftapi\controller;

use app\BaseController;
use app\service\propertyService;
use app\service\zftService;
use think\facade\Db;
use think\facade\Log;

class Trade extends BaseController{
    public function index(){
        echo "222";
    }
    //订单号固定创建订单
    public function tradeSnMoney(){
        return ;
        $out_trade_no=request()->post("sn");
        $gateid=request()->post("gate_id");
        $money=request()->post("money");
        $gateinfo=Db::table("community_zft_gate")->where("id",$gateid)->find();
        if(empty($gateinfo)){
            parent::sendOutput("小区配置不存在",500);
            parent::sendOutput(['info' => '小区配置不存在'],500);
        }
        $propertyZftInfo=(new propertyService())->propertyZftByComId($gateinfo["community_id"]);
        if(empty($propertyZftInfo)){
            parent::sendOutput("直付通配置为空",500);
            parent::sendOutput(['info' => '直付通配置为空'],500);
            return json(['info' => '直付通配置为空']);
        }

        $info["property_id"]=$propertyZftInfo["property_id"];
        $info["order_money"]=request()->post("money");
        $info["remark"]=request()->post("remark");
        $info["user_id"]=$this->request->openid;
        $info["community_name"]=$gateinfo["community_name"];
        $info["pay_money"]=request()->post("money");
        $info["out_trade_no"]=$out_trade_no;
        $info["subject"] = $gateinfo["community_name"]."-".$gateinfo["gate_name"];
        $info["community_id"]=$gateinfo["community_id"];
        $info["gate_name"]=$gateinfo["gate_name"];
        $info["gate_id"]=$gateinfo["id"];


        $api = new zftService();

        $notifyUrl=$this->getCurrentUrl()."/zftpay/callback/notice";

        $settle_detail_info=array(
            'trans_in_type'=>'defaultSettle',//'loginName',
            'trans_in'=>'',//收款账号aliwykd@163.com
            'amount'=>$info["pay_money"],//结算金额
        );
        $data= array(
            'out_trade_no'=> $info["out_trade_no"],
            'total_amount'=>$info["pay_money"],
            'buyer_id'=>$info["user_id"],//$info["user_id"],
            'subject' =>$info["subject"],
            'notifyurl'=>$notifyUrl,
            'merchant_id'=>$propertyZftInfo["merchant_id"],
            'notifyUrl'=>$notifyUrl,
            'settle_detail_info'=>$settle_detail_info,
        );
        $result = $api->create(json_encode($data));

        Log::write($result);
        if(property_exists($result,'sub_msg')){
            parent::sendOutput($result->sub_msg,500);
            return json(['info' => $result->sub_msg]);
        }else{
            $info['trade_no'] =  $result->trade_no;
            $res=Db::table("zft_order")->insert($info);
            //返回结果到前台唤起收银台
            if($res){
                $data["trade_no"]=$info["trade_no"];
                parent::sendOutput($data);
                //return json(['trade_no' => $info['trade_no']]);
            }else{
                parent::sendOutput("订单存储失败",500);
                return json(['info' => '订单存储失败',"code"=>500]);
            }
        }
    }

    public function trade(){
        return ;
        $gateid=request()->post("gate_id");
        $money=request()->post("money");
        $gateinfo=Db::table("community_zft_gate")->where("id",$gateid)->find();
        if(empty($gateinfo)){
            parent::sendOutput(['info' => '小区配置不存在'],500);
        }
        $propertyZftInfo=(new propertyService())->propertyZftByComId($gateinfo["community_id"]);
        if(empty($propertyZftInfo)){
            parent::sendOutput(['info' => '直付通配置为空'],500);
            return json(['info' => '直付通配置为空']);
        }

        $info["property_id"]=$propertyZftInfo["property_id"];
        $info["order_money"]=request()->post("money");
        $info["remark"]=request()->post("remark");
        $info["user_id"]=$this->request->openid;
        $info["community_name"]=$gateinfo["community_name"];
        $info["pay_money"]=request()->post("money");
        $info["out_trade_no"]=date('YmdHiss').rand(100000,999999);
        $info["subject"] = $gateinfo["community_name"]."-".$gateinfo["gate_name"];
        $info["community_id"]=$gateinfo["community_id"];
        $info["gate_name"]=$gateinfo["gate_name"];
        $info["gate_id"]=$gateinfo["id"];


        $api = new zftService();

        $notifyUrl=$this->getCurrentUrl()."/zftpay/callback/notice";

        $settle_detail_info=array(
            'trans_in_type'=>'defaultSettle',//'loginName',
            'trans_in'=>'',//收款账号aliwykd@163.com
            'amount'=>$info["pay_money"],//结算金额
        );
        $data= array(
            'out_trade_no'=> $info["out_trade_no"],
            'total_amount'=>$info["pay_money"],
            'buyer_id'=>$info["user_id"],//$info["user_id"],
            'subject' =>$info["subject"],
            'notifyurl'=>$notifyUrl,
            'merchant_id'=>$propertyZftInfo["merchant_id"],
            'notifyUrl'=>$notifyUrl,
            'settle_detail_info'=>$settle_detail_info,
        );
        $result = $api->create(json_encode($data));

        Log::write($result);
        if(property_exists($result,'sub_msg')){
            parent::sendOutput($result->sub_msg,500);
            return json(['info' => $result->sub_msg]);
        }else{
            $info['trade_no'] =  $result->trade_no;
            $res=Db::table("zft_order")->insert($info);
            //返回结果到前台唤起收银台
            if($res){
                $data["trade_no"]=$info["trade_no"];
                parent::sendOutput($data);
                //return json(['trade_no' => $info['trade_no']]);
            }else{
                parent::sendOutput("订单存储失败",500);
                return json(['info' => '订单存储失败',"code"=>500]);
            }
        }
    }
}